package com.ls.fw.hadoop.client.util;

import java.util.IdentityHashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ls.fw.hadoop.client.constant.DatanodeJMXEnum;
import com.ls.fw.hadoop.client.constant.NamenodeJMXEnum;
import com.ls.fw.hadoop.client.constant.StateType;
import com.ls.fw.net.httpclient.utils.HttpclientUtil;

public class HadoopClientUtils {

	/**
	 * 获取节点状态
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static StateType getState(String url) throws Exception{
		String state = "";
		Map<String,String> paraMap  = new IdentityHashMap<String,String>();
		paraMap.put(QRY, NamenodeJMXEnum.STATUS.code());
		String content =  HttpclientUtil.get(url, paraMap);
		JSONObject  obj = (JSONObject) JSON.parse(content);
		JSONArray array = obj.getJSONArray("beans");
		if(array!=null){
			JSONObject  namenode = (JSONObject) array.get(0);
			state = namenode.getString("State");
		}
		return StateType.getByCode(state);
	}
	public static final String QRY = "qry";
	public static String namenodeJVM(String url) throws Exception{
		Map<String,String> paraMap  = new IdentityHashMap<String,String>();
		paraMap.put(QRY, NamenodeJMXEnum.JvmMetrics.code());
		return HttpclientUtil.get(url, paraMap);
	}
	public static String namenodeSystem(String url) throws Exception{
		Map<String,String> paraMap  = new IdentityHashMap<String,String>();
		paraMap.put(QRY, NamenodeJMXEnum.OperatingSystem.code());
		return HttpclientUtil.get(url, paraMap);
	}
	public static String namenodeInfo(String url) throws Exception{
		Map<String,String> paraMap  = new IdentityHashMap<String,String>();
		paraMap.put(new String(QRY), NamenodeJMXEnum.INFO.code());
		return HttpclientUtil.get(url, paraMap);
	}

	public static String namenodeStatus(String url) throws Exception{
		Map<String,String> paraMap  = new IdentityHashMap<String,String>();
		paraMap.put(new String(QRY), NamenodeJMXEnum.STATUS.code());
		return HttpclientUtil.get(url, paraMap);
	}
	
	public static String datanodeJVM(String url) throws Exception{
		Map<String,String> paraMap  = new IdentityHashMap<String,String>();
		paraMap.put(QRY, DatanodeJMXEnum.JvmMetrics.code());
		return HttpclientUtil.get(url, paraMap);
	}
	public static String datanodeSystem(String url) throws Exception{
		Map<String,String> paraMap  = new IdentityHashMap<String,String>();
		paraMap.put(QRY, DatanodeJMXEnum.OperatingSystem.code());
		return HttpclientUtil.get(url, paraMap);
	}
	public static String datanodeInfo(String url) throws Exception{
		Map<String,String> paraMap  = new IdentityHashMap<String,String>();
		paraMap.put(new String(QRY), DatanodeJMXEnum.INFO.code());
		return HttpclientUtil.get(url, paraMap);
	}
	public static String datanodeStatus(String url) throws Exception{
		Map<String,String> paraMap  = new IdentityHashMap<String,String>();
		paraMap.put(new String(QRY), DatanodeJMXEnum.STATUS.code());
		return HttpclientUtil.get(url, paraMap);
	}
}
